package com.wd.health.utils;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.viewpager2.widget.ViewPager2;

import com.wd.health.model.api.Api;
import com.wd.health.app.App;
import com.wd.health.model.bean.BannerBean;
import com.wd.health.model.bean.BarrageBean;
import com.wd.health.model.bean.ByqBean;
import com.wd.health.model.bean.ByqxqBean;
import com.wd.health.model.bean.BzxqBean;
import com.wd.health.model.bean.CyypBean;
import com.wd.health.model.bean.CyypBean02;
import com.wd.health.model.bean.DanMuBean;
import com.wd.health.model.bean.DoctorBean;
import com.wd.health.model.bean.DqwzBean;
import com.wd.health.model.bean.FaceLoginBean;
import com.wd.health.model.bean.HealthJtBean;
import com.wd.health.model.bean.HistoryMoneyBean;
import com.wd.health.model.bean.HistoryWzBean;
import com.wd.health.model.bean.JkzxBean01;
import com.wd.health.model.bean.CjbzBean02;
import com.wd.health.model.bean.KslbBean;
import com.wd.health.model.bean.LoginBean;
import com.wd.health.model.bean.LswzBean;
import com.wd.health.model.bean.MessageStatusBean;
import com.wd.health.model.bean.MoneyBean;
import com.wd.health.model.bean.PopularBean;
import com.wd.health.model.bean.RegisterBean;
import com.wd.health.model.bean.SearchBean;
import com.wd.health.model.bean.SendMessageBean;
import com.wd.health.model.bean.SfSignInBean;
import com.wd.health.model.bean.SignInBean;
import com.wd.health.model.bean.StopBean;
import com.wd.health.model.bean.TalkBean;
import com.wd.health.model.bean.UpdatePwdBean;
import com.wd.health.model.bean.UploadAvaterBean;
import com.wd.health.model.bean.UserTaskBean;
import com.wd.health.model.bean.VideoInfoBean;
import com.wd.health.model.bean.WXzhifuBean;
import com.wd.health.model.bean.WxLoginBean;
import com.wd.health.model.bean.WzysBean;
import com.wd.health.model.bean.YpxqBean;
import com.wd.health.model.bean.YqmBean;
import com.wd.health.model.bean.ZfbBean;
import com.wd.health.model.bean.ZixunBean;
import com.wd.health.model.bean.ZixunyishengBean;
import com.wd.health.model.bean.ZxxqBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Query;

/**
 * @ProjectName:shopmall
 * @Package:com.bawei.myhealth.utils
 * @ClassName:RetrofitUtils
 * @UpdateUser: 裴童童
 * @date: 2020/12/7
 */
public class RetrofitManager {
    private static RetrofitManager retrofitManager;
    private Retrofit retrofit;
    private final ApiService apiService;

    private RetrofitManager(){
        //日志拦截器
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .addInterceptor(new Myheaders())
                .build();
        retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        apiService = retrofit.create(ApiService.class);
    }

    private class Myheaders implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            SharedPreferences user = App.getAppContext().getSharedPreferences("user", Context.MODE_PRIVATE);
            int userId = user.getInt("userId", 0);
            String sessionId = user.getString("sessionId", "");
            Request request = chain.request();
            Request build = request.newBuilder()
                    .addHeader("userId", String.valueOf(userId))
                    .addHeader("sessionId", sessionId)
                    .build();
            return chain.proceed(build);
        }
    }

    //单例
    public static RetrofitManager getInstance(){
        if (retrofitManager == null) {
            synchronized (RetrofitManager.class){
                if (retrofitManager == null) {
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }
    //注册
    public void postRegisterData(String email, String code, String pwd1, String pwd2, Observer<RegisterBean> observer){
        apiService.RegisterData(email, code, pwd1, pwd2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //登陆
    public void postLoginData(String email, String pwd, Observer<LoginBean> observer){
        apiService.LoginData(email,pwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //发送邮箱验证码
    public void postEmailData(String email , Observer<RegisterBean> observer){
        apiService.getEmailData(email)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //banner
    public void getBannerData(Observer<BannerBean> observer){
        apiService.getBannerData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //科室列表
    public void getKslbData(Observer<KslbBean> observer){
        apiService.getKszx()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //健康资讯一级条目
    public void getJkzx01Data(Observer<JkzxBean01> observer){
        apiService.getJkzxData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //常用药品一级列表
    public void getCyypData(Observer<CyypBean> observer){
        apiService.getCyypData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //常见病症二级列表
    public void getCjbz02Data(int departmentId, Observer<CjbzBean02> observer){
        apiService.getJkzxData02(departmentId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //常用药品二级列表
    public void getCyyp02Data(int drugsCategoryId, int page, int count, Observer<CyypBean02> observer){
        apiService.getCyypData02(drugsCategoryId,page,count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //资讯列表
    public void getZixunData(int plateId, int page, int count, Observer<ZixunBean> observer){
        apiService.getZiXunData(plateId,page,count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //病症详情
    public void getBzxqData(int id, Observer<BzxqBean> observer){
        apiService.getBzxqData(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //药品详情
    public void getYpxqData(int id, Observer<YpxqBean> observer){
        apiService.getYpxqData(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //搜索
    public void getSearchData(String keyWord, Observer<SearchBean> observer){
        apiService.getSearchData(keyWord)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //热门搜索
    public void getPopData(Observer<PopularBean> observer){
        apiService.getPopData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //问诊医生列表
    public void getWzysData(int deptId,int condition,int page,int count,Observer<WzysBean> observer){
        apiService.getWzysData(deptId,condition,page,count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //咨询详情
    public void getZxxqData(int infoId, Observer<ZxxqBean> observer){
        apiService.getZxxqData(infoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //病友圈
    public void getByqData(int departmentId, int page, int count, Observer<ByqBean> observer){
        apiService.getByqData(departmentId,page,count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //病友圈详情
    public void getByqxqData(int sickCircleId, Observer<ByqxqBean> observer){
        apiService.getByqxqData(sickCircleId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //医生明细
    public void getDoctorData(int doctorId, Observer<DoctorBean> observer){
        apiService.getDoctorData(doctorId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //咨询医生
    public void putZixunData(int doctorId, Observer<ZixunyishengBean> observer){
        apiService.putZixunData(doctorId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //当前问诊
    public void getDqwzData(Observer<DqwzBean> observer){
        apiService.getDqwzData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //结束问诊
    public void putStopData(int recordId,Observer<StopBean> observer){
        apiService.putStopWzData(recordId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //发送消息
    public void postSendData(int inquiryId,String content,int type,int doctorId,Observer<SendMessageBean> observer){
        apiService.postSendData(inquiryId,content,type,doctorId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //查询消息记录
    public void getTalkData(int inquiryId, int page, int count, Observer<TalkBean> observer){
        apiService.getTalkData(inquiryId, page, count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //查询历史记录
    public void getHistoryData(int page, int count, Observer<LswzBean> observer){
        apiService.getHistroyData(page, count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //根据视频类目查询视频列表
    public void getVideoInfoData(int categoryId, int page, int count, Observer<VideoInfoBean> observer){
        apiService.getVideoInfoData(categoryId,page,count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //我的钱包
    public void getMoneyData(Observer<MoneyBean> observer){
        apiService.getMoneyData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //微信支付
    public void postWXpay(double money, int payType, Observer<WXzhifuBean> observer){
        apiService.postWXzhifu(money,payType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //查询健康讲堂类目
    public void getHealthJt(Observer<HealthJtBean> observer){
        apiService.getHealthjtData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //查询视频评论（弹幕）
    public void getDanMuData(int videoId,Observer<DanMuBean> observer){
        apiService.getDanMuData(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //查看历史问诊
    public void getHistoryWzData(int page, int count, Observer<HistoryWzBean> observer){
        apiService.getHistorywenzhenData(page,count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //查看消费记录
    public void getHistoryMoneyData(int page, int count, Observer<HistoryMoneyBean> observer){
        apiService.getHistoryMoneyData(page,count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //用户签到
    public void postSignInData(Observer<SignInBean> observer){
        apiService.postSigninData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //查看用户当天是否签到
    public void getSigninData(Observer<SfSignInBean> observer){
        apiService.getSignInData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //查询用户任务列表
    public void getUserTaskData(Observer<UserTaskBean> observer){
        apiService.getUserTaskListData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //上传头像
    public void postAvaterData(File image, Observer<UploadAvaterBean> observer){
        apiService.postAvaterData(image)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //微信登录
    public void postWxLoginDate(String wxCode, Observer<WxLoginBean> observer){
        apiService.postWxLoginData(wxCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //修改密码
    public void putUpdatePwdData(String oldPwd, String newPwd, Observer<UpdatePwdBean> observer){
        apiService.putUpdatePwdData(oldPwd, newPwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //支付宝支付
    public void postZfbData(double money, int payType, Observer<ZfbBean> observer){
        apiService.postZfbData(money, payType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //完善邮箱
    public void putMailData(String email, Observer<MessageStatusBean> observer){
        apiService.putMailData(email)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //生成邀请码
    public void postYqmData(Observer<MessageStatusBean> observer){
        apiService.postYQMData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //查询用户邀请码
    public void getYHYqmData(Observer<YqmBean> observer){
        apiService.getYHYQMData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //绑定人脸
    public void postFaceData(String faceImage,Observer<MessageStatusBean> observer){
        HashMap<String,String> map = new HashMap<>();
        map.put("faceImage",faceImage);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), new JSONObject(map).toString());
        apiService.postFaceData(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //绑定身份证
    public void postSfzData(int userId,String name,String sex,String nation,String birthday,String address,String idNumber,String issueOffice,Observer<MessageStatusBean> observer){

        HashMap<String,String> map = new HashMap<>();
        map.put("userId", String.valueOf(userId));
        map.put("name",name);
        map.put("sex",sex);
        map.put("nation",nation);
        map.put("birthday",birthday);
        map.put("address",address);
        map.put("idNumber",idNumber);
        map.put("issueOffice",issueOffice);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), String.valueOf(new JSONObject(map)));
        apiService.postSfzData(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //绑定银行卡
    public void postYhkData(String bankCardNumber,String bankName,int bankCardType,Observer<MessageStatusBean> observer){
        apiService.postYhkData(bankCardNumber,bankName,bankCardType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //人脸登录
    public void postFaceLoginData(String faceImage, Observer<FaceLoginBean> observer){
        apiService.postFaceLoginData(faceImage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    //弹幕
    public void getBarrageData(int videoId, Observer<BarrageBean> observer){
        apiService.getBarrageData(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}
